package stackoverflow.domain.question;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import junit.framework.Assert;

import org.junit.Test;

import stackoverflow.domain.user.User;
import stackoverflow.domain.user.UserUtility;


public class QuestionTests
{
	private static final String userName ="userForQuestionTests";
	private static final String password ="passwordForQuestionTests";	
	
	@Test
	(expected=java.lang.IllegalArgumentException.class)
	public void questionMustHaveAtLeastOneTagWhenCreated()
	{
		User postedByUser = UserUtility.createNewUser(userName, password);
		Question.QuestionBuilder questionBuilder = 
			new Question.QuestionBuilder(postedByUser).postText("Some random text.");
		Question question = questionBuilder.build();
	}

	@Test
	public void questionCanHaveMultipleTagsWhenCreated()
	{
		Set<Tag> tags = PostUtility.createTags("tag1", "tag2");
//		Question question = PostUtility.createQuestionWithMultipleTags("tag1", "tag2");
		Question question = PostUtility.createQuestionWithTags(tags);
		List<Tag> inputTags= new ArrayList<Tag>();
		inputTags.add(new Tag("tag1"));
		inputTags.add(new Tag("tag2"));
		Assert.assertTrue("Question can have multiple tags", question.getTags().containsAll(inputTags));
	}
	
	@Test
	public void questionCanAddTagsAfterCreation()
	{
		Set<Tag> tags = PostUtility.createTags("tag1");
		Question question = PostUtility.createQuestionWithTags(tags);
		Tag tag2= new Tag("tag2");
		question.addTag(tag2);
		Assert.assertTrue("Tags can be added after creation.",question.getTags().contains(tag2));
	}
	
	@Test(expected=java.lang.UnsupportedOperationException.class)
	public void questionReturnsUnmodifiableListOfTags()
	{
		Set<Tag> tags = PostUtility.createTags("tag1");
		Question question = PostUtility.createQuestionWithTags(tags);
		question.getTags().remove(new Tag("tag"));
	}

	@Test
	public void cannotAddTwiceTheSameTagToQuestion()
	{
		Set<Tag> tags = PostUtility.createTags("tag1","tag2");
		Question question = PostUtility.createQuestionWithTags(tags);
		question.addTag(new Tag("tag2"));
		Assert.assertEquals("The tag2 should occur once, so tag size remains 2", 2, question.getTags().size());
	}
	
	@Test
	(expected=java.lang.IllegalArgumentException.class)
	public void everyQuestionNeedsAUserAtCreationTime()
	{
		Question.QuestionBuilder questionBuilder = 
			new Question.QuestionBuilder(null).postText("Some random text.");
		Question question = questionBuilder.build();
	}

}
